function bindEvent(elem, type, fn) {
    elem.addEventListener(type, fn)
}

// 完整版绑定方法
function bindFun(elem, type, selector, fn) {
    if (fn == null) {
        fn = selector
        selector = null
    }
    elem.addEventListener(type, event => {
        const target = event.target
        if (selector) {
            // 代理绑定
            if (target.matches(selector)) {
                fn.call(target, event)
            }
        } else {
            // 普通绑定
            fn.call(target, event)
        }
    })
}

const a = document.getElementById('div1')
bindFun(a, 'click', function (e) {
    e.preventDefault()  // 阻止默认行为
    console.log(e.target) // 返回触发的元素
    console.log(this) // 返回触发的元素
    alert(this.innerHTML)
})
const b = document.getElementById('div2')
bindFun(b, 'click', 'div', function (e) {
    alert(this.innerHTML)
})

